package com.zmykj.hik.rocketMQ;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.UnsupportedEncodingException;

/**
 * 生产者配置(自启动)
 * @author LengChen
 * @version 1.0
 * @date 2021/5/24
 */
@Configuration
public class RocketMqProducer {

    /**
     * 发送同一类消息的设置为同一个group，保证唯一,默认不需要设置，rocketmq会使用ip@pid(pid代表jvm名字)作为唯一标示
     */
    /*@Value("${rocketmq.producer.groupName}")*/
    private  String groupName = "GID_hikMessage";
    @Value("${rocketmq.producer.namesrvAddr}")
    private  String namesrvAddr;


    private final static Logger logger = LoggerFactory.getLogger(RocketMqProducer.class);

    private static DefaultMQProducer defaultMQProducer;

    /**
     * Spring bean init-method
     */
    @PostConstruct
    public void init() throws MQClientException {
        // 参数信息
        logger.info("DefaultMQProducer initialize!");
        logger.info(groupName);
        logger.info(namesrvAddr);

        // 初始化
        defaultMQProducer = new DefaultMQProducer(groupName);
        defaultMQProducer.setNamesrvAddr(namesrvAddr);
        defaultMQProducer.setInstanceName(String.valueOf(System.currentTimeMillis()));

        defaultMQProducer.start();

        logger.info("DefaultMQProudcer start success!");

    }

    /**
     * Spring bean destroy-method
     */
    @PreDestroy
    public void destroy() {
        defaultMQProducer.shutdown();
    }

    public DefaultMQProducer getDefaultMQProducer() {
        return defaultMQProducer;
    }

    // ---------------setter -----------------

    public  void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public  void setNamesrvAddr(String namesrvAddr) {
        this.namesrvAddr = namesrvAddr;
    }

    /**
     * rocketmq发送消息方法
     *
     * @param topic 组名
     * @param tagName 同一个topic下的不同 分支,同一个消费者只能取一个组的下的不同的tag分支
     * @param key 保持唯一
     * @param msgBody 消息体
     * @return
     */
    public static void sendMsgIntime(String topic, String tagName, String key, String msgBody) {
        Message msg = null;
        try {
            msg = new Message(topic,tagName,key,msgBody.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            String result = defaultMQProducer.send(msg).toString();
            logger.info("send rockmq topic:" + topic + " " + result);
        } catch (Exception e) {
            logger.error("send rockmq error topic:" + topic + new String(msgBody), e);
        }
    }


    public static void main(String[] args) {
        System.out.println("123456");
    }
}
